package controller;

import implementor.BalancePaymentDaoImplementor;
import implementor.ColorImp;
import implementor.MaterialImp;
import implementor.PurchaseImp;
import implementor.SellImp;
import implementor.SellerInfoImp;
import implementor.SizeImp;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.SQLException;
import java.util.Vector;

import javax.swing.JOptionPane;
import javax.swing.table.DefaultTableModel;

import daoInterface.BalancePaymentDao;
import daoInterface.ColorDao;
import daoInterface.MaterialDao;
import daoInterface.PurchaseDao;
import daoInterface.SellerInfoDao;
import daoInterface.SizeDao;
import dbhandeling.Crude;
import dto.BalancePayment;
import dto.Color;
import dto.Material;
import dto.Purchase;
import dto.SellerInfo;
import dto.Size;
import utilities.AppUtilites;
import utilities.Constants;
import utilities.ViewUtilites;
import views.inventory.PrintBarCode;

public class PrintBarCodeController implements ActionListener
{
	private PrintBarCode view;
	int srno=0;
	double total=0;
	String sellername;
	int quantity;
	double mrp;
	double purchaseprice;
	String size;
	String color;
	String category;
	DefaultTableModel dtm;
	BalancePayment bp;
	BalancePaymentDao bpd;
	Purchase p;
	private Vector<Material> materials;
	Material m;
	
	public PrintBarCodeController() 
	{
	
	}
	
	public PrintBarCodeController(PrintBarCode view) 
	{
		super();
		this.view = view;
	
		view.addActionListenerOnButtons(this);
		
		AppUtilites.setSellerNameCombo(view.getCbSellerName());
		AppUtilites.setCategoryCombo(view.getCbCategoryName());
		AppUtilites.setSizeCombo(view.getCbSize());
		AppUtilites.setColorCombo(view.getCbColor());
		
		materials=new Vector<Material>();
	}
	
	public PrintBarCode getView() 
	{
		return view;
	}

	public void setView(PrintBarCode view) 
	{
		this.view = view;	
	}

	@Override
	public void actionPerformed(ActionEvent e) 
	{
		String event=e.getActionCommand();
		
		if(event.contains("print barcode"))
		{
			reactOnPrintBarcode();
		}
		
		if(event.contains("add stock"))
		{
			try 
			{
				reactOnAddStock();
			} 
			catch (SQLException e1) 
			{
				e1.printStackTrace();
			} 
			catch (Exception e1) 
			{
				e1.printStackTrace();
			}
		}
		
		if(event.contains("add material"))
		{
			try 
			{
				reactOnAddMaterial();
			} 
			catch (SQLException e1) 
			{
				e1.printStackTrace();
			} 
			catch (Exception e1) 
			{
				e1.printStackTrace();
			}
			
			reactOnClear();
		}
		
		if(event.contains("cl"))
		{
			ViewUtilites.makeDateComponent(view.getTfDate());
		}
		if(event.contains("clear"))
		{
			reactOnClear();
		}
	}
	/*******************************************************************************/
	public void reactOnPrintBarcode()
	{
		
	}
	/*******************************************************************************/
	public void reactOnClear()
	{
		view.getTfQuantity().setText("");
		view.getTfmrp().setText("");
		view.getTfPurchasePrice().setText("");
		view.getCbCategoryName().setSelectedIndex(0);
		view.getCbColor().setSelectedIndex(0);
		view.getCbSellerName().setSelectedIndex(0);
		view.getCbSize().setSelectedIndex(0);
	}
	/*******************************************************************************/
	public void reactOnAddStock() throws SQLException, Exception
	{
		int k=addBalancePaymentObject();
		/**********************************/
		k=addPurchaseObject(k);
		/**********************************/
		MaterialDao md=new MaterialImp();
		for(int i=0;i<materials.size();i++)
		{
			Material temp=materials.get(i);
			temp.setpurchaseId(k);
			md.add(temp);
		}
		JOptionPane.showMessageDialog(null,"Stock added successfully.");
	}
	/**
	 * @throws Exception 
	 * @throws SQLException *****************************************************************************/
	public int addBalancePaymentObject() throws SQLException, Exception
	{
		bp=new BalancePayment();
		bp.settotalAmt(total);
		bp.setamtPaid(total);
		bp.setbalanceDate(ViewUtilites.convert(view.getTfDate().getText()));
		bp.settype(Constants.EXPENSE);
		
		bpd=new BalancePaymentDaoImplementor();
		bpd.add(bp);
		
		return bpd.getAutoGenratedKey();
	}
	/**
	 * @throws Exception 
	 * @throws SQLException *****************************************************************************/
	public int addPurchaseObject(int k) throws SQLException, Exception
	{
		p=new Purchase();
		p.setsellerId(AppUtilites.getSellerId(sellername));
		p.setBalancePaymentId(k);
		p.setpurchaseDate(view.getTfDate().getText());
		p.settaxCode("purchasexxx");
		
		PurchaseDao pd=new PurchaseImp();
		pd.add(p);
		
		return p.getautoPurchaseId();
	}
	/**
	 * @throws Exception 
	 * @throws SQLException *****************************************************************************/
	public void reactOnAddMaterial() throws SQLException, Exception
	{
		sellername=view.getCbSellerName().getSelectedItem().toString();
		category=view.getCbCategoryName().getSelectedItem().toString();
		quantity=Integer.parseInt(view.getTfQuantity().getText());
		purchaseprice=Double.parseDouble(view.getTfPurchasePrice().getText());
		mrp=Double.parseDouble(view.getTfmrp().getText());
		size=view.getCbSize().getSelectedItem().toString();
		color=view.getCbColor().getSelectedItem().toString();
		
		dtm=(DefaultTableModel) view.getTable().getModel();
		
		srno=srno+1;
		dtm.addRow(new Object[]{srno,sellername,category,size,color,purchaseprice,quantity,mrp});
		total=total+quantity*purchaseprice;
		view.getTfTotal().setText(total+"");
		
		m=new Material();
		
		m.setCatId(AppUtilites.getCategoryId(category));
		m.setsizeId(AppUtilites.getSizeId(size));
		m.setcolorId(AppUtilites.getColorId(color));
		m.setmQuant(quantity);
		m.setmMrp(mrp);
		m.setpurchaseQuant(quantity);
		m.setpurchasePrice(purchaseprice);
		m.settaxCode("matxxx");
		
		materials.add(m);
	}
	/*******************************************************************************/
}
